import { defineStore } from "pinia";
import { ref, computed } from "vue";
import { searchBlogs, searchUsers } from "@/api/search";

export const useBlogStore = defineStore("blog", () => {
    const blogData = ref({ list: [] });
    //const blogList = ref([]);
    const getBlogs = async (
        searchText,
        searchType = "blog",
        page = 1,
        pageSize = 10
    ) => {
        try {
            const searchApi = searchType === "blog" ? searchBlogs : searchUsers;
            const res = await searchApi({
                keyword: searchText,
                page,
                pageSize,
            });
            // TODO: 处理搜索结果
            if (res.code === "200") {
                blogData.value.list = res.data.list;
                return res;
            } else {
                console.error("搜索结果返回错误:", res);
            }
        } catch (error) {
            console.error("搜索失败:", error);
        }
    };

    const initializeEmptySearch = () => {
        return getBlogs("", "blog", 1, 10);
    };
    return {
        getBlogs: getBlogs,
        blogData: computed(() => blogData.value),
        //blogList: computed(() => blogData.value.list),
        initializeEmptySearch: initializeEmptySearch,
    };
});
